<template>
  <!-- 休息日设定表单弹窗 -->
  <el-dialog
    :close-on-click-modal="false"
    v-model="visibleDialog"
    :title="props.title"
    width="50%"
    draggable
    @close="closeDialog"
  >
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="100px">
      <el-row>
        <el-col :span="10"
          ><el-form-item label="日历名称" prop="freeDayName">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.freeDayName"
              placeholder="请输入日历安排名称"
              clearable
            />

            <span v-else>{{ formData.freeDayName }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="休息日类型" prop="freeDayType">
            <el-select
              v-if="!props.isReadonly"
              v-model="formData.freeDayType"
              placeholder="请选择休息日类型"
              clearable
            >
              <el-option
                v-for="item in dictionaryStore.filterDictList('FreeDayType')"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>

            <span v-else>{{
              dictionaryStore.filterDictName(
                "FreeDayType",
                formData.freeDayType
              )
            }}</span>
          </el-form-item></el-col
        >
      </el-row>
      <el-row>
        <el-col :span="10">
          <el-form-item label="起始日期" prop="beginDate">
            <template v-if="!props.isReadonly">
              <el-date-picker
                v-model="formData.beginDate"
                type="date"
                placeholder="请选择起始日期"
                clearable
              />
            </template>
            <template v-else>
              <span>{{ formData.beginDate }}</span>
            </template>
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="截止日期" prop="endDate">
            <template v-if="!props.isReadonly">
              <el-date-picker
                v-model="formData.endDate"
                type="date"
                placeholder="请选择截止日期"
                clearable
              />
            </template>
            <template v-else>
              <span>{{ formData.endDate }}</span>
            </template>
          </el-form-item>
        </el-col>
      </el-row>

      <el-form-item label="是否允许加班" prop="allowOvertime">
        <template v-if="!props.isReadonly">
          <el-radio-group v-model="formData.allowOvertime">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="0">否</el-radio>
          </el-radio-group>
        </template>
        <span v-else>{{
          dictionaryStore.filterDictName("YesNo", formData.allowOvertime)
        }}</span>
      </el-form-item>

      <el-form-item
        v-if="props.isReadonly !== false"
        label="创建时间"
        prop="createTime"
      >
        <span>{{ formData.createTime }}</span>
      </el-form-item>
      <el-form-item
        v-if="props.isReadonly !== false"
        label="创建人名"
        prop="createdUserName"
      >
        <span>{{ formData.createdUserName }}</span>
      </el-form-item>
      <el-form-item
        v-if="props.isReadonly !== false"
        label="更新时间"
        prop="lastUpdateTime"
      >
        <span>{{ formData.lastUpdateTime }}</span>
      </el-form-item>
      <el-form-item
        v-if="props.isReadonly !== false"
        label="更新人名"
        prop="lastUpdatedUserName"
      >
        <span>{{ formData.lastUpdatedUserName }}</span>
      </el-form-item>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确 定</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "MDMMDMFREEDAYADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
  }
);

const rules = reactive({
  workCalendarId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  freeDayName: [
    { required: true, message: "请输入日历安排名称", trigger: "blur" },
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],

  beginDate: [{ required: true, message: "请选择起始日期", trigger: "blur" }],
  endDate: [{ required: true, message: "请选择截止日期", trigger: "blur" }],
  freeDayType: [
    { required: true, message: "请选择休息日类型", trigger: "blur" },
  ],
  allowOvertime: [],
  remarks: [
    { min: 1, max: 255, message: "长度在 1 到 255 个字符", trigger: "blur" },
  ],
  createTime: [],
  createdUserName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  lastUpdateTime: [],
  lastUpdatedUserName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  workCalendarId: "",
  freeDayName: "",
  beginDate: "",
  endDate: "",
  freeDayType: "",
  allowOvertime: 1,
  version: "",
  remarks: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  deleted: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    formData.workCalendarId = props.data.toString();
    if (newVal?.id) {
      const {
        id,
        workCalendarId = props.data.toString(),
        freeDayName,
        beginDate,
        endDate,
        freeDayType,
        allowOvertime,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        deleted,
      } = newVal;
      Object.assign(formData, {
        id,
        workCalendarId,
        freeDayName,
        beginDate,
        endDate,
        freeDayType,
        allowOvertime,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        deleted,
      });
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = "";
  formData.workCalendarId = "";
  formData.freeDayName = "";
  formData.beginDate = "";
  formData.endDate = "";
  formData.freeDayType = "";
  formData.allowOvertime = 1;
  formData.version = "";
  formData.remarks = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
  formData.deleted = "";
}

const loading = ref(false);

/** 休息日设定保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (formData.beginDate > formData.endDate) {
      ElMessage.error(`起始日期不能晚于截止日期`);
      return;
    }
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/mdm/mdmfreeday/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
</script>
